package com.situ.mallbrand.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.util.JsonResult;
import com.mall.model.Product;
import com.situ.mallbrand.model.Brand;
import com.situ.mallbrand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
* 品牌控制器*/
@RestController
@RequestMapping("/api/brands")
public class BrandController {
    @Autowired
    private BrandService brandService;




    /*
    * 新增品牌*/
    @PostMapping
    public JsonResult<Boolean> create(@RequestBody Brand brand) {
        return JsonResult.success(brandService.create(brand));
    }

    /*
    * 修改品牌*/
    @PutMapping("/{id}")
    public JsonResult<Boolean> updateById(@PathVariable("id") Long id, @RequestBody Brand brand) {
        brand.setId(id);
        return JsonResult.success(brandService.updateById(brand));
    }

    @GetMapping
    public JsonResult<Page<Brand>> list(
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size) {
        return JsonResult.success(brandService.pageQuery(keyword, status, page, size));
    }

    @DeleteMapping("/{id}")
    public JsonResult<Boolean> delete(@PathVariable("id") Long id) {
        return JsonResult.success(brandService.softDelete(id));
    }

    @GetMapping("/{id}/products")
    public ResponseEntity<JsonResult<?>>getProductByBrand(@PathVariable("id") Long id) {

        return ResponseEntity.ok(JsonResult.success(brandService.getProductsByBrand(id)));
    }

    @GetMapping("/list")
    public ResponseEntity< JsonResult<?>> getBrandList() {
        return ResponseEntity.ok(JsonResult.success(brandService.getBrandList()));
    }

}
